﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;


namespace Fishes
{
    class Fish
    {
        //Fish Properties
        private int currentHunger;
        private int totalHunger;

        private float m_health;

        private int m_age;
        private int adultAge;
        
        private int m_fishType;
        public int seed;

        public Texture2D fishTexture;
        Vector2 position;
        Vector2 size;
        Vector2 direction;
        Random randnums;
        
        public Fish(int type)
        {
            fishTexture = loadTextureByType(type);
            position = Vector2.Zero;
            size = new Vector2(50f);
            m_age = 0;
            m_health = 100;
            adultAge = loadAgeByType(type);
        }
        public Fish(int type, Texture2D text, int seed)
        {
            fishTexture = text;
            int directionPointX;
            int directionPointY;
            randnums = new Random(seed);
            directionPointX = randnums.Next(-1, 1);
            directionPointY = randnums.Next(-1, 1);
            if (directionPointX == 0)
                directionPointX = 1;

            if (directionPointY == 0)
                directionPointY = 1;
            position = new Vector2(randnums.Next(300, 300), randnums.Next(300, 300));
            direction = new Vector2((float)randnums.NextDouble()* 3 * directionPointX, (float)randnums.NextDouble() *1 *directionPointY);
            if (direction.X == 0)
                direction.X = 1;
            if (direction.Y == 0)
                direction.Y = 1;


            size = new Vector2(50f);
            m_age = 0;
            m_health = 100;
            adultAge = loadAgeByType(type);
        }
        private int loadAgeByType(int type)
        {
            //TODO: Fill this out.
            switch (type)
            {
                default:
                    return 3;
            }
        }
        private int loadHungerByType(int type)
        {
            //TODO: Fill this out
            switch (type)
            {
                default:
                    return 30;
            }
        }
        private Texture2D loadTextureByType(int type)
        {
            //TODO: Fill this out
            
            switch (type)
            {
                default:
                    return null;
            }
        }
        //TODO: We may want to return here and make this based on a number, not percentage.
        public float hunger
        {
            get
            {
                return currentHunger / totalHunger;
            }
        }
        public void feed(int amount)
        {
            if (currentHunger + amount < totalHunger)
                currentHunger += amount;
            else
                currentHunger = totalHunger;
           
        }
        public void move(int height, int width)
        {
            position.X += direction.X;
            position.Y += direction.Y;
            if (position.X <= 0 || position.Y <= 0 || position.X >= width || position.Y >= height)
            {
                int polarityX = 1;
                int polarityY = 1;
                if (direction.X >= 0)
                    polarityX = -1;
                if (direction.Y >= 0)
                    polarityY = -1;
                direction.X = (float)randnums.NextDouble() * 3 * polarityX;
                direction.Y = (float)randnums.NextDouble() * polarityY;
                if (direction.X == 0)
                    direction.X = 1;
                if (direction.Y == 0)
                    direction.Y = 1;
                return;
                //direction = new Vector2((float)randnums.NextDouble() * 3 * directionPointX, (float)randnums.NextDouble() * 1 * directionPointY);
            }
            position.X += direction.X;
            position.Y += direction.Y;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            float rotation = 0f;
            SpriteEffects effects = SpriteEffects.FlipHorizontally;
            if (direction.X >= 0)
                effects = SpriteEffects.None;
            rotation = (float)Math.Atan(direction.Y / direction.X);

            spriteBatch.Draw(fishTexture, position, null, Color.White, rotation, Vector2.Zero, 0.25f, effects, 0);
        }

    }
}
